package org.jetlinks.protocol.official.mqtt.down.radar60FL.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.hswebframework.web.exception.BusinessException;
import org.jetlinks.core.message.function.FunctionInvokeMessage;
import org.jetlinks.protocol.official.ObjectMappers;
import org.jetlinks.protocol.official.TopicPayload;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author bingzhong
 * @Date 2024/12/31 13:43
 * @Description 跌倒灵敏度设置
 */
public class Radar60FLFallSensitivity {

    static ObjectMapper mapper = ObjectMappers.JSON_MAPPER;

    @SneakyThrows
    public static TopicPayload encode(FunctionInvokeMessage functionInvokeMessage, String topicPatten) {

        String value = String.valueOf(functionInvokeMessage
                                                .getInput("value")
                                                .orElseThrow(() -> new BusinessException("value 不能为空")));

        String topic = String.format(topicPatten, functionInvokeMessage.getDeviceId());
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("version", "1.0");
        dataMap.put("method", "set");
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("fallSensitivity", value);
        dataMap.put("params", paramMap);
        byte[] payloadBytes = mapper.writeValueAsBytes(dataMap);

        return TopicPayload.of(topic, payloadBytes);

    }

}
